package loginview;

import View.View;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import static users.LoginFun.showJDialog;
import static users.LoginFun.userContain;

public class Login extends JFrame implements MouseListener {
    JPanel p0=new JPanel();
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JPanel p4=new JPanel();
    JPanel p5=new JPanel();
    JPanel p6=new JPanel();

    JLabel welcome=new JLabel("欢迎来到网上书店系统");
    JLabel username=new JLabel("账号:   ");
    JLabel password=new JLabel("密码:   ");
    JLabel codeText=new JLabel("验证码:");
    JLabel status=new JLabel("选择身份:  ");

    JTextField usernametext=new JTextField(15);
    JPasswordField passwordField=new JPasswordField(15);

    //添加验证码输入框
    JTextField codeInput=new JTextField(10);
    //生成随机验证码
    String codeStr= CodeUnit.getCode();
    JLabel rightCode=new JLabel(codeStr);

    JComboBox<String> comboBox = new JComboBox<>();

    JButton confirm=new JButton("登录");
    JButton register=new JButton("注册");
    JButton exit=new JButton("退出");

    String selected="普通用户";

    public Login(){
        //初始化界面
        initJFrame();
        //在这个界面添加内容
        initView();
        //让页面展示出来
        this.setVisible(true);

    }

    private void initJFrame() {
        this.setTitle("网上书店系统");
        this.setSize(600, 430);
        this.setLayout(new GridLayout(8, 1,10,10));
        //设置页面置顶
        //this.setAlwaysOnTop(true);
        //设置页面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private void initView() {

        welcome.setFont(new Font("微软雅黑", Font.BOLD, 22));
        Font size=new Font("微软雅黑", Font.PLAIN, 15);
        username.setFont(size);
        password.setFont(size);
        codeText.setFont(size);
        status.setFont(size);
        rightCode.setFont(new Font("微软雅黑", Font.BOLD, 13));

        comboBox.addItem("普通用户");
        comboBox.addItem("管理员");
        comboBox.addItem("游客(审核未通过)");

        p1.add(welcome);
        p2.add(username);
        p2.add(usernametext);
        p3.add(password);
        p3.add(passwordField);
        p4.add(codeText);
        p4.add(codeInput);
        p4.add(rightCode);
        p5.add(status);
        p5.add(comboBox);
        p6.add(confirm);
        p6.add(register);
        p6.add(exit);




        this.add(p0);
        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        this.add(p5);
        this.add(p6);

        //验证码鼠标监听
        rightCode.addMouseListener(this);

        //登录按钮监听
        confirm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object obj=e.getSource();
                if(obj==confirm){
                    //获取用户输入的用户名和密码
                    String username=usernametext.getText();
                    char[] passwordChars = passwordField.getPassword();
                    String password=new String(passwordChars);
                    String code=codeInput.getText();
                    //System.out.println(username+","+password);

                    //验证码判断
                    if(code.isEmpty()){
                        showJDialog("验证码为空");
                        refreshCode();
                        return;
                    }

                    if(!(code.equalsIgnoreCase(codeStr))){
                        showJDialog("验证码错误");
                        refreshCode();
                        return;
                    }

                    //判断用户名或密码输入是否为空
                    if(username.isEmpty()||password.isEmpty()){
                        showJDialog("用户名或密码为空，请重新输入");
                        //清空用户名输入框和密码输入框
                        //usernameInput.setText("");
                        //passwordInput.setText("");
                        refreshCode();
                        return;
                    }

                    //判断用户名和密码是否正确
                    try {
                    if(selected.equals("普通用户")){
                        boolean flag1=userContain(username,password,"0");
                        if(flag1){
                            Login.this.setVisible(false);
                            new View(username);
                        }else{
                            showJDialog("登录失败，用户名或密码或身份错误");
                            refreshCode();
                        }
                    }else if(selected.equals("管理员")){
                        boolean flag2=userContain(username,password,"1");
                        if(flag2){
                            Login.this.setVisible(false);
                            new View(username);
                        }else{
                            showJDialog("登录失败，用户名或密码或身份错误");
                            refreshCode();

                        }
                    }else if(selected.equals("游客(审核未通过)")){
                        boolean flag3=userContain(username,password,"-1");
                        if(flag3){
                            Login.this.setVisible(false);
                            new View(username);
                        }else{
                            showJDialog("登录失败，用户名或密码或身份错误");
                            refreshCode();
                        }
                    }
                    }catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }



                }

            }
        });

        //注册按钮监听
        register.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Login.this.setVisible(false);
                new Register();
            }
        });

        //退出按钮监听
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        // 添加复选框监听
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    // 获取并打印选中的项
                    selected = (String) comboBox.getSelectedItem();
                    System.out.println("选中的内容: " + selected);
                }
            }
        });

    }

    private void refreshCode() {
        // 生成新的随机验证码
        String newCode = CodeUnit.getCode();

        // 更新验证码标签显示
        codeStr=newCode;
        rightCode.setText(newCode);
        rightCode.repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj=e.getSource();
        if(obj==rightCode){
            refreshCode();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
